<%@ page session="false" buffer="256kb"%>
<jsp:directive.page import="org.opencms.i18n.CmsEncoder"/>
<jsp:directive.page import="org.opencms.jsp.CmsJspActionElement"/>
<jsp:directive.page import="org.opencms.file.CmsObject"/>
<jsp:directive.page import="java.text.SimpleDateFormat"/>
<jsp:directive.page import="org.springframework.web.bind.ServletRequestUtils"/>
<jsp:directive.page import="com.dcampus.opencms.search.Searcher"/>
<jsp:directive.page import="org.opencms.search.CmsSearchIndex"/>
<jsp:directive.page import="org.opencms.search.CmsSearchResultList"/>
<jsp:directive.page import="org.opencms.main.OpenCms"/>
<jsp:directive.page import="java.util.*"/>
<jsp:directive.page import="org.opencms.search.CmsSearchResult"/>
<jsp:directive.page import="org.opencms.file.CmsResource"/>
<jsp:directive.page import="org.opencms.file.CmsResourceFilter"/>
<%@ page import="org.opencms.file.CmsVfsResourceNotFoundException"%>
<%@ page import="com.dcampus.contentmodels.CmsTemplateResolver" %>
<%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="dc" uri="http://www.dcampus.com/taglib/opencms"%><%   
    
	// Create a JSP action element
    CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
    CmsTemplateResolver.initPageContextAttribute(cms);
    
	CmsObject co = cms.getCmsObject();
	// Get the search manager
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	String indexName = cms.getRequestContext().currentProject().isOnlineProject() ?
			"Online project ZH (VFS)" : "Offline project ZH (VFS)";
	String uri = cms.getRequestContext().getUri();
	String resourcename = uri.substring(0, uri.indexOf("/", 1) + 1);
	
	String[] fields = new String[]{"title", "content", "keywords"};
	
	String query = ServletRequestUtils.getStringParameter(request, "query");
	
	int pageSize = 20;
	//Searcher.Source source = new Searcher.Source(resourcename, null, true);
	Searcher.Source source = new Searcher.Source((String)pageContext.getAttribute("__SITE_FOLDER__"), null, true);
	
	int searchPage = ServletRequestUtils.getIntParameter(request, "_page_", 1);
	int size = 0;
	CmsSearchResultList list = null;
	if (query != null && query.length() > 0) {
		Searcher.Parameter parameter = new Searcher.Parameter();
		parameter.addSource(source);
		
		//parameter.setSorts(Searcher.SORT_DATE_RELEASED_DESC);
		parameter.setPage(searchPage);
		parameter.setSize(pageSize);
		parameter.setFields(fields);
		parameter.setQuery(query);
		//结果条目的摘要的段落数
		parameter.setFragmentCount(10);
		//parameter.addQuery("title",query);
		CmsSearchIndex index = OpenCms.getSearchManager().getIndex(indexName);
		list = Searcher.search(co, index, parameter);
		size = list.getHitCount();
	}
	pageContext.setAttribute("query", query);
	
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>网站搜索_<cms:contentload collector="singleFile" param="${__SITE_FOLDER__}index.html"  editable="false"><cms:contentshow element="Title" /></cms:contentload></title>
<link href="<cms:link>${__SITE_FOLDER__}-themes-/theme1/main.css</cms:link>" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/resources/commons/PagingTag.js"></script>
<cms:editable/>
</head>

<%
String uri2 = cms.getCmsObject().getRequestContext().getUri();
pageContext.setAttribute("requestFolder", uri.substring(0, uri.lastIndexOf("/")));
pageContext.setAttribute("uri", uri2);
String rootUri = uri.substring(0, uri.indexOf("/", 1));
pageContext.setAttribute("rootFolder", rootUri );
%>
</head>

<body>
<div id="wrap">
  <cms:include file="${__SITE_FOLDER__}-themes-/theme1/head.jsp" cacheable="false" /> 
  
  <div id="menu">
      <cms:include file="${__SITE_FOLDER__}-navigation-/menu.jsp" cacheable="false"/>
    <div class="clear"></div>
  </div>
  <div class="hr10"></div>
  
<div class="searchcontent">
  
  <div class="serachWrap">
    <form method="get" action="">
   <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="left"><input type="text" value="<c:out value="${query }" escapeXml="true"/>" size="50" name="query" id="searchname"/> <input type="submit" value="搜索"  id="searchsubmit"/></td>
  </tr>
</table>
  </form>
  </div>

  <div class="searchTitle">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="10" align="center"></td>
        <td class="title">搜索结果</td>
        <td class="countsearch">搜索获得约 <span style="color:#f00;"><%=size%></span> 条结果</td>
      </tr>
    </table>
  </div>

<%
   if (size > 0) {
	   pageContext.setAttribute("itemsCount", size);
	   
	   List<Map<String, Object>> entryList = new ArrayList<Map<String, Object>>();
	   for(CmsSearchResult entry : list) {
		   String filePath = co.getRequestContext().removeSiteRoot(entry.getPath());
		   String hrefPath = cms.link(filePath);
           CmsResource _res = null;
           try{
        	   _res = co.readResource(filePath, CmsResourceFilter.ALL);
           }catch(CmsVfsResourceNotFoundException e){
        	   //FIXME
        	   continue;
           }
           String title = entry.getTitle();
           if (title == null) {
           	title = _res.getName();
           }
           if (title != null) {
	            String red = "<span class='red'>"+query+"</span>";
	        	title = title.replaceAll(query, red);
           }
           String deployTime = "";
	   	    if (!(_res.getDateReleased() == CmsResource.DATE_RELEASED_DEFAULT)) {
	   	    	deployTime = format.format(new Date(_res.getDateReleased()));
	   	    }
	   	    Map<String, Object> map = new HashMap<String, Object>();
	   	    map.put("deployTime", deployTime);
	   	 	map.put("title", title);
	   		map.put("hrefPath", hrefPath);
	   		map.put("entry", entry);
	   		entryList.add(map);
	 }
	 pageContext.setAttribute("items", entryList); 
%>
<div class="searchResultWrap"> 
  <dc:pageregion htmlId="pageregion" delay="false" size="20">
    <dc:paging htmlId="paging" rootTag="ul" var="item" navigator="info" items="${items}" itemsCount="${itemsCount}"> 
	  <li>
        <h3><a target="_blank" href="${item.hrefPath }">${item.title}</a> <span class="gray">(结果相关度:${item.entry.score}%)</span></h3>
        <div class="summary2"> ${dc:showHtml(dc:substring(item.entry.excerpt, 0, 400, "..."), true, false, false)} </div>
        <div class="d">发布日期：${item.deployTime}</div>
      </li>
    </dc:paging>
              
    <div class="page_detail">	
	<span id="info" style="display:none" class="page-info">共{pageCount}页 {itemCount}条记录
	 <a href="javascript:paging.prev()" class="page-prev">上页</a>{navigator}<a href="javascript:paging.next()" class="page-next">下页</a> 
	</span>
	</div>
	
  </dc:pageregion>
</div>

<div class="serachWrap">
   <form method="get" action="">
   <table width="100%" border="0" cellspacing="0" cellpadding="0">
     <tr>
       <td align="left"><input type="text" value="<c:out value="${query }" escapeXml="true"/>" size="50" name="query" id="searchname"/> <input type="submit" value="搜索"  id="searchsubmit"/></td>
     </tr>
   </table>
  </form>
</div>
<%} %> 
  

  </div><!-- searchcontent -->

  <div class="hr10"></div>
  <div class="copyright">
  <cms:contentload collector="singleFile" param="${__SITE_FOLDER__}-modules-/copyright.html" editable="true">
      	<cms:contentshow element="Text" />
  </cms:contentload>
  </div>


</div>
</body>
</html>